package user.factory;

import book.Library;
import constant.Constant;
import user.AdminUser;
import user.NormalUser;
import user.User;
import utils.PermissionException;

public class ProxyUser {
    private User realUser;

    private Library library;

    public ProxyUser(User user) {
        this.realUser = user;
        library = Library.getLibrary();
    }

    public User getRealUser() {
        return realUser;
    }

    public void setRealUser(User realUser) {
        this.realUser = realUser;
    }

    public int display() {
        return this.realUser.display();
    }

    // -------------------- 管理员方法 --------------------------
    private void checkRealUserWhetherAdminUser(String message) {
        if (!(this.realUser instanceof AdminUser)) {
            throw new PermissionException(message);
        }
    }

    //添加图书
    public void addBook() {
        System.out.println("代理类的addBook类方法执行了");
        try {
            checkRealUserWhetherNormalUser("普通用户没有此权限");
        } catch (PermissionException e) {
            e.printStackTrace();
        }


    }

    //图书修改 支持修改书名 作者 类别
    public void updateBook() {

    }

    //删除书籍
    public void removeBook() {

    }

    //统计每本书的借阅次数
    public void borrowCount() {

    }

    //查询最受欢迎的前n本书
    public void generateBook() {

    }

    //查看库存状态
    public void checkInventoryStatus() {

    }

    //并移除上架超过一年的图书
    public void checkAndRemoveOldBooks() {

    }

    //业务代码的实现
    public void exit() {

    }

    // -------------------- 普通用户方法 --------------------------
    private void checkRealUserWhetherNormalUser(String message) {
        if (!(this.realUser instanceof NormalUser)) {
            throw new PermissionException(message);
        }
    }

    //借阅图书
    public void borrowBook() {

    }


    //归还图书
    public void returnBook() {

    }

    // 查看个人借阅情况
    public void viewBorrowBooks() {

    }

    public void handleOperation(int choice) {

        if (this.realUser instanceof AdminUser) {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.ADD_BOOK:
                    addBook();
                    break;
                case Constant.UPDATE_BOOK:
                    updateBook();
                    break;
                case Constant.REMOVE_BOOK:
                    removeBook();
                    break;
                case Constant.BORROWED_BOOK_COUNT:
                    borrowCount();
                    break;
                case Constant.GENERATE_BOOK:
                    generateBook();
                    break;
                case Constant.CHECK_INVENTORY_STATUS:
                    checkInventoryStatus();
                    break;
                case Constant.CHECK_AND_REMOVE_OLD_BOOK:
                    checkAndRemoveOldBooks();
                default:
                    break;
            }
        } else {
            switch (choice) {
                case Constant.SEARCH_BOOK:
                    library.searchBook();
                    break;
                case Constant.DISPLAY_BOOK:
                    library.displayBooks();
                    break;
                case Constant.EXIT:
                    library.exit();
                    break;
                case Constant.BORROWED_BOOK:
                    borrowBook();
                    break;
                case Constant.RETURN_BOOK:
                    returnBook();
                    break;
                case Constant.VIEW_BORROW_HISTORY_BOOK:
                    viewBorrowBooks();
                    break;
            }
        }
    }
}
